//
//  TableViewController.m
//  oneSelect
//
//  Created by satoshi_uma on 2020/6/18.
//  Copyright © 2020 satoshi_uma. All rights reserved.
//
#define SCREEN_WIDTH               ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT              ([UIScreen mainScreen].bounds.size.height)

/*
   获取画iPhone nav和tabbar高度
 */
//#define mc_Is_iphone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define mc_Is_iphone               ([[UIDevice currentDevice] userInterfaceIdiom]  == UIUserInterfaceIdiomPhone)
#define mc_Is_iphoneX              SCREEN_WIDTH >= 375.0f && SCREEN_HEIGHT >= 812.0f && mc_Is_iphone

/*状态栏高度*/
#define mcStatusBarHeight          (CGFloat)(mc_Is_iphoneX ? (44.0) : (20.0))
/*导航栏高度*/
#define mcNavBarHeight             (44)
/*状态栏和导航栏总高度*/
#define mcNavBarAndStatusBarHeight (CGFloat)(mc_Is_iphoneX ? (88.0) : (64.0))
/*TabBar 高度*/
#define mcTabBarHeight             (CGFloat)(mc_Is_iphoneX ? (49.0 + 34.0) : (49.0))
/*顶部安全区域远离高度*/
#define mcTopBarSafeHeight         (CGFloat)(mc_Is_iphoneX ? (44.0) : (0))
/*TabBar 底部安全区域远离高度*/
#define mcBottomSafeHeight         (CGFloat)(mc_Is_iphoneX ? (34.0) : (0))
/*iPhoneX的状态栏高度差值*/
#define mcTopBarDifHeight          (CGFloat)(mc_Is_iphoneX ? (24.0) : (0))
/*导航条和Tabbar总高度*/
#define mcNavAndTabHeight          (mcNavBarAndStatusBarHeight + mcTabBarHeight)

#import "TableViewController.h"
#import "TableViewCell.h"
#import "DetailViewController.h"

typedef NS_ENUM (NSInteger, MC_Pay) {
    mc_payUnknown = -1, // 不知道
    mc_payALiPay,       // 支付宝
    mc_payWeChatPay,    // 微信
    mc_payYinLian       // 银联
};

@interface TableViewController ()
{
    UIButton *payBtn;
}
@property (nonatomic, strong) NSArray *payArr;
@property (nonatomic, assign) MC_Pay mc_pay;
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _payArr = @[@"支付宝", @"微信", @"银联"];

    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:@"TableViewCell" bundle:nil] forCellReuseIdentifier:@"payID"];

    payBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - mcTabBarHeight - mcStatusBarHeight, SCREEN_WIDTH, mcTabBarHeight)];
    [payBtn setTitle:@"支付" forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(payClick) forControlEvents:UIControlEventTouchUpInside];
    [self setPayBtn];
    [self.view addSubview:payBtn];
}

- (void)setPayBtn{
    [payBtn setBackgroundColor:[UIColor grayColor]];
    payBtn.userInteractionEnabled = NO;
}

- (void)payClick {
    switch (_mc_pay) {
        case mc_payUnknown:
            NSLog(@"mc_pay=-1");
            break;
        case mc_payALiPay:
            NSLog(@"mc_pay=0");
            break;
        case mc_payWeChatPay:
            NSLog(@"mc_pay=1");
            break;
        case mc_payYinLian:
            NSLog(@"mc_pay=2");
            break;

        default:
            break;
    }
    DetailViewController *detail = [[DetailViewController alloc]init];
    [self presentViewController:detail animated:YES completion:nil];
    //    [self.navigationController pushViewController:detail animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"payID";
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setPayTitle:_payArr[indexPath.row]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _mc_pay = indexPath.row;

    if (_mc_pay != mc_payUnknown) {
        payBtn.userInteractionEnabled = YES;
        [payBtn setBackgroundColor:[UIColor redColor]];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
////    [self.dataArray removeObjectAtIndex:indexPath.row];
//    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//}
//
//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return @"返回";
//}

//- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) {
//        // 收回侧滑
//        [tableView setEditing:NO animated:YES];
//    }];
//
//
//    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *_Nonnull action, NSIndexPath *_Nonnull indexPath) {
//        // 删除cell: 必须要先删除数据源，才能删除cell
////        [self.dataArray removeObjectAtIndex:indexPath.row];
////        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        [tableView setEditing:NO animated:YES];
//    }];
//
//    return @[deleteAction, editAction];
//}

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    UIContextualAction *editAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"编辑" handler:^(UIContextualAction *_Nonnull action, __kindof UIView *_Nonnull sourceView, void (^_Nonnull completionHandler)(BOOL)) {
        // 不做任何事，只是收回侧滑
        [tableView setEditing:NO animated:YES];
        [self setPayBtn];
        completionHandler(YES);
    }];

    editAction.backgroundColor = [UIColor greenColor];

    UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[editAction]];

    return configuration;
}

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIContextualAction *editAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"编辑" handler:^(UIContextualAction *_Nonnull action, __kindof UIView *_Nonnull sourceView, void (^_Nonnull completionHandler)(BOOL)) {
        [self setPayBtn];
        // 不做任何事，只是收回侧滑
        [tableView setEditing:NO animated:YES];
        completionHandler(YES);
    }];

    editAction.backgroundColor = [UIColor grayColor];

    UIContextualAction *deletAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"删除" handler:^(UIContextualAction *_Nonnull action, __kindof UIView *_Nonnull sourceView, void (^_Nonnull completionHandler)(BOOL)) {
        
        DetailViewController *detail = [[DetailViewController alloc]init];
        [self presentViewController:detail animated:YES completion:nil];
        [self setPayBtn];
        // 不做任何事，只是收回侧滑
//        [tableView setEditing:NO animated:YES];
        completionHandler(YES);
    }];

    
    UISwipeActionsConfiguration *configuration = [UISwipeActionsConfiguration configurationWithActions:@[deletAction,editAction]];
    [self setPayBtn];
    return configuration;
}

@end
